package com.example.com.transcribeeditpublish.views.paster;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hanszhli on 2017/6/22.
 * <p>
 * 用于统一管理{@link TCLayerOperationView}的layout
 */
public class TCLayerViewGroup extends FrameLayout implements View.OnClickListener {
    private List<TCLayerOperationView> mChilds;
    private int mLastSelectedPos = -1;


    public TCLayerViewGroup(Context context) {
        super(context);
        init();
    }

    public TCLayerViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TCLayerViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public int getmLastSelectedPos() {
        return mLastSelectedPos;
    }

    private void init() {
        mChilds = new ArrayList<TCLayerOperationView>();
    }

    public void addOperationView(TCLayerOperationView view) {
        mChilds.add(view);
        selectOperationView(mChilds.size() - 1);
        addView(view);
        view.setOnClickListener(this);
    }

    public void removeOperationView(TCLayerOperationView view) {
        int viewIndex = mChilds.indexOf(view);
        mChilds.remove(view);
        mLastSelectedPos = -1;
        removeView(view);
        view.setOnClickListener(null);
    }

    public TCLayerOperationView getOperationView(int index) {
        return mChilds.get(index);
    }


    public void selectOperationView(int pos) {
        if (pos < mChilds.size()) {
            if (mLastSelectedPos != -1) {
                TCLayerOperationView view = mChilds.get(mLastSelectedPos);
                view.setEditable(false);//不显示编辑的边框
                view.setVisibilityTopLeft(view.getTop(), view.getLeft());
                view.setVisibility(INVISIBLE);//不显示编辑的边框
            }
            if (pos != -1) {
                mChilds.get(pos).setEditable(true);//显示编辑的边框
                mChilds.get(pos).setVisibility(VISIBLE);
            }
            mLastSelectedPos = pos;
        }
    }

    private void unSelectOperationView(int pos) {
        if (pos < mChilds.size() && mLastSelectedPos != -1) {
            mChilds.get(mLastSelectedPos).setEditable(false);//不显示编辑的边框
            mLastSelectedPos = -1;
        }
    }

    public TCLayerOperationView getSelectedLayerOperationView() {
        if (mLastSelectedPos < 0 || mLastSelectedPos >= mChilds.size()) return null;
        return mChilds.get(mLastSelectedPos);
    }

    public int getSelectedViewIndex() {
        return mLastSelectedPos;
    }

    public int getChildCount() {
        return mChilds.size();
    }

    @Override
    public void onClick(View v) {
        TCLayerOperationView tcLayerOperationView = (TCLayerOperationView) v;
        int pos = mChilds.indexOf(tcLayerOperationView);
        if (pos == mLastSelectedPos) {
            return;
        }
        int lastPos = mLastSelectedPos;
        selectOperationView(pos); //选中编辑
        if (mListener != null) {
            mListener.onLayerOperationViewItemClick(tcLayerOperationView, lastPos, pos);
        }
    }

    private float startx, starty;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startx = event.getX();
                starty = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                float x = event.getX();
                float y = event.getY();
                if (Math.abs(startx - x) < 10 && Math.abs(y - starty) < 10) {
                    for (int i = mChilds.size() - 1; i >= 0; i--) {
                        TCLayerOperationView tcView = mChilds.get(i);
                        if (tcView.isContainer(x, y)) {
                            tcView.performClick();
                            return true;
                        }
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onLayerOperationViewItemClick(TCLayerOperationView view, int lastSelectedPos, int currentSelectedPos);
    }

}
